package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.adapter.ViewPagerAdapter;
import com.wsc.wisdomchinaqinghe.constant.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jaily.zhang
 * @version V1.3.1
 * @Description 引导界面
 * @date 2014-7-23 下午3:30:39
 */
public class GuideActivity extends BaseActivity implements OnClickListener{

    private View bootPager1;
    private View bootPager2;
    private View bootPagerStart;// 引导页1,2,3,
    private ViewPager viewPager;// viewpager 控件
    private List<View> listView;// View 页面的集合
    private TextView tv_lookinto;
    private Button bt_login;
    private Button bt_regist;

    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_layout);
        initView();
        context=GuideActivity.this;
    }

    /**
     * @Description 初始化控件
     * @author jaily.zhang
     */
    private void initView() {

        viewPager = (ViewPager) findViewById(R.id.viewPager);// viewPager 初始化
        iv1 = (ImageView) findViewById(R.id.iv_1);
        iv2 = (ImageView) findViewById(R.id.iv_2);
        iv3 = (ImageView) findViewById(R.id.iv_3);

        LayoutInflater lf = getLayoutInflater().from(this);// 布局拓展页
        bootPager1 = lf.inflate(R.layout.bootpager1_layout, null);// 引导页1 初始化
        bootPager2 = lf.inflate(R.layout.bootpager2_layout, null);//引导页2初始化
        bootPagerStart = lf.inflate(R.layout.bootpagerstart_layout, null);

        tv_lookinto = (TextView) bootPagerStart.findViewById(R.id.tv_lookinto);
        tv_lookinto.setOnClickListener(this);

        bt_login = (Button) bootPagerStart.findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);

        bt_regist = (Button) bootPagerStart.findViewById(R.id.bt_regist);
        bt_regist.setOnClickListener(this);

        listView = new ArrayList<View>();// View List 集合初始化
        listView.add(bootPager1);// 添加引导页1
        listView.add(bootPager2);// 添加引导页1
        listView.add(bootPagerStart);
        viewPager.setAdapter(new ViewPagerAdapter(listView, this, bootPagerStart));// 设置viewpageradapter
        viewPager.setCurrentItem(0);// 显示引导页1
        viewPager.setOnPageChangeListener(new MyPageChangeListener());// viwePager
        // 页面选中事件
    }

    /**
     * @author jaily.zhang
     * @version V1.3.1
     * @Description viewPager 选中事件
     * @date 2014-7-23 下午3:16:47
     */
    class MyPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            switch (arg0) {
                // 选中第一页，点变红色
                case 0:
                    resetColor();
                    iv1.setImageResource(R.drawable.guideselectedoval);
                    break;
                // 选中第二页 ，第二个点变红色
                case 1:
                    resetColor();
                    iv2.setImageResource(R.drawable.guideselectedoval);
                    break;
                case 2:
                    resetColor();
                    iv3.setImageResource(R.drawable.guideselectedoval);
                    break;

            }
        }

    }

    private void resetColor() {
        iv1.setImageResource(R.drawable.guideunselectedoval);
        iv2.setImageResource(R.drawable.guideunselectedoval);
        iv3.setImageResource(R.drawable.guideunselectedoval);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            // 随便看看
            case R.id.tv_lookinto:
                Tools.setSharedPreferencesboolen(Constant.isFirstIn, false, context);
                // 进入到主界面
                Intent intent = new Intent(this, HomeActivity.class);
                startActivity(intent);
                finish();
                break;

            // 登录app
            case R.id.bt_login:
                Intent intentLogin = new Intent(this,LoginActivity.class);
                startActivityForResult(intentLogin,0);
                break;
            //注册app
            case R.id.bt_regist:
                Intent intentRegist = new Intent(this,SMSActivity.class);
                startActivity(intentRegist);
//                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (resultCode == 1) {
                Intent intent=new Intent(context,HomeActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }
}